// pages/add/mood/mood.js
Page({
  data: {
    message: "",
    fileList: [],
    imgIds: [],
    btn_status: true
  },
  onLoad: function (options) {
    wx.enableAlertBeforeUnload({
      message: "确定退出编辑吗?~"
    });
  },
  upload(e) {
    getApp().globalData.util.imgsUpload(this, e, this.data.fileList, this.data.imgIds);
  },
  delete(e) {
    let i = e.detail.index;
    let fileList = this.data.fileList;
    let imgIds = this.data.imgIds;
    fileList.splice(i, 1);
    imgIds.splice(i, 1);
    this.setData({
      fileList: fileList,
      imgIds: imgIds
    });
  },
  submit() {
    if (this.data.btn_status) {
      if (!this.data.message) {
        getApp().globalData.vant.toast("心情动态内容不能为空");
      } else {
        this.setData({
          btn_status: false
        });
        getApp().globalData.http.squarePost({
          type: 1,
          content: this.data.message,
          imgIds: this.data.imgIds.join(",")
        }).then(res => {
          getApp().globalData.vant.toast({
            type: "success",
            forbidClick: true,
            message: "发布成功",
            onClose: () => {
              wx.switchTab({
                url: "/pages/home/home",
                success: function (e) {
                  var page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                  page.reset();
                }
              });
            }
          });
        }).catch(e => {
          this.setData({
            btn_status: true
          });
        });
      }
    }
  }
})